﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
    public class LabcheckDAL : ILabcheck
    {
        #region 实验室指标增删改查基本操作
        public bool AddLabcheck(LabcheckInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.LabcheckSP_AddLabcheck,
                info.Name, info.IsQuantified, info.MeasurementUnits, info.LimitValues, info.NormalValues, info.ChoicesDescription, 
                info.Intro, info.Desp, info.UpdateTime, info.GridValues, info.ShortName);
            return cnt > 0;
        }

        public bool UpdateLabcheck(LabcheckInfo info)
        {
            int cnt = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.LabcheckSP_UpdateLabcheck,
                info.Id, info.Name, info.IsQuantified, info.MeasurementUnits, info.LimitValues, info.NormalValues, info.ChoicesDescription,
                info.Intro, info.Desp, info.UpdateTime, info.GridValues, info.ShortName);
            return cnt > 0;
        }

        public bool DeleteLabcheck(int[] itemIds)
        {
            int cnt = itemIds.Length;
            if (cnt <= 0) return false;
            StringBuilder sb = new StringBuilder();
            sb.Append("[id] in (");
            sb.Append(itemIds[0]);

            for (int i = 1; i < cnt; i++)
            {
                sb.Append(", ");
                sb.Append(itemIds[i]);
            }
            sb.Append(")");

            int cnt2 = SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.LabcheckSP_DelLabcheck,
                sb.ToString());
            return (cnt == cnt2);
        }

        public LabcheckInfo GetLabcheckInfo(int id)
        {
            IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
                Common.LabcheckSP_GetLabcheck,
                id) as IDataReader;
            return CBO.FillObject<LabcheckInfo>(reader);
        }
        #endregion 
 
        #region 实验室指标列表以及搜索操作
        private List<LabcheckInfo> GetLabcheckList(int pageSize, int pageIndex, string condstr, string orderstr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.LabcheckSP_GetLabcheckList,
                pageSize, pageIndex, condstr, orderstr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<LabcheckInfo>(dr);
        }

        private List<LabcheckInfo> GetLabcheckListByDisease(int pageSize, int pageIndex, string condstr, string orderstr, out int count)
        {
            DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
                Common.LabcheckSP_GetLabcheckListByDisease,
                pageSize, pageIndex, condstr, orderstr);

            IDataReader dr = dset.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                count = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                count = 0;
            }

            dr = dset.Tables[1].CreateDataReader();
            return CBO.FillCollection<LabcheckInfo>(dr);
        }

        /*检索所有实验室指标*/
        public List<LabcheckInfo> GetLabcheckList(int pageSize, int pageIndex, out int count)
        {
            return GetLabcheckList(pageSize, pageIndex, "", " [Labcheck].[Id] desc ", out count);
        }

        /*按照疾病Id检索对应的实验室指标列表*/
        public List<LabcheckInfo> GetLabcheckList(int diseaseId, int pageSize, int pageIndex, out int count)
        {
            string condstr = string.Format(@" [DiseaseLabcheck].[DiseaseId] = {0} ", diseaseId);
            return GetLabcheckListByDisease(pageSize, pageIndex, condstr, " [Labcheck].[Id] desc ", out count);
        }

        /*按照实验室指标名称来检索症状*/
        public List<LabcheckInfo> GetLabcheckByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condstr = string.Format(@" [indicatorname] like '%{0}%' ", keyword);
            return GetLabcheckList(pageSize, pageIndex, condstr, " [Labcheck].[Id] desc ", out count);
        }
        #endregion
    }
}
